﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Read.Api.Handler.Imuntites
{
    public class EochImuntitableCommandHandler : IRequestHandler<BackImuntitableCommand, ApiResult>
    {
        private readonly IImunitysRepository _immunitysRepository;

        private readonly ILogger<EochImuntitableCommandHandler> _logger;

        private readonly IMapper _mapper;

        public EochImuntitableCommandHandler(IImunitysRepository immunitysRepository, ILogger<EochImuntitableCommandHandler> logger, IMapper mapper)
        {
            _immunitysRepository = immunitysRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(BackImuntitableCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var immunity = await _immunitysRepository.GetAsync(x=>x.ImunityId== request.ImunityId);
                if (immunity.ImunityId == null)
                {
                    return ApiResult.Fail( "回显失败");
                }                
                return  ApiResult.Ok(immunity);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Back immunity failed");
                return  ApiResult.Fail( "Back immunity failed");
            }
        }
    }
}
public class BackImuntitableCommand:IRequest<ApiResult>
{
    public int ImunityId { get; set; }
}